package cn.wolfcode.luowowo;

import cn.wolfcode.luowowo.website.resolver.UserInfoResolver;
import cn.wolfcode.luowowo.website.web.intercepter.CheckLoginIntercepter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@SpringBootApplication
public class WebSiteServer implements WebMvcConfigurer{

    @Bean
    public CheckLoginIntercepter checkLoginIntercepter(){
        return new CheckLoginIntercepter();
    }


    //配置自定义解析器
    @Bean
    public UserInfoResolver userInfoResolver(){
        return new UserInfoResolver();
    }

    //从WebMvcConfigurer中获取方法
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userInfoResolver());
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginIntercepter())
                .addPathPatterns("/**")
                //除了静态和备注的方法不拦,其它全部都拦,贴了CheckLoginAnnotion标签的就检查,其余放行

                .excludePathPatterns("/checkPhone")
                .excludePathPatterns("/sendVerifyCode")
                .excludePathPatterns("/userRegist")
                .excludePathPatterns("/userLogin")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/regist.html")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/styles/**");

    }

    public static void main(String[] args) {
        SpringApplication.run(WebSiteServer.class,args);
    }
}
